package com.regvision.contacts.ui.adapter;

import java.util.List;

import com.regvision.contacts.R;
import com.regvision.contacts.domain.PhoneModel;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PhoneAdapter extends BaseAdapter implements OnClickListener {

	private Context mContext;
	private int resource;
	private PhoneType mPhoneType;
	private List<PhoneModel> list;
	
	public enum PhoneType {
		Phone,	//有拨打电话
		NoPhone//无拨打电话
	}

	public PhoneAdapter(Context context, int resource, List<PhoneModel> list) {
		this(context, PhoneType.Phone, resource, list);
	}
	public PhoneAdapter(Context context, PhoneType type, int resource, List<PhoneModel> list) {
		this.mContext = context;
		this.resource = resource;
		this.list = list;
		this.mPhoneType = type;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Holder holder;
		PhoneModel model = (PhoneModel)getItem(position);
		
		if (convertView == null) {
			convertView = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(resource, null);
			holder = new Holder();
			
			holder.tvName = (TextView) convertView.findViewById(R.id.tv_item_phone_name);
			holder.tvNumber = (TextView) convertView.findViewById(R.id.tv_item_phone_number);
			holder.ivCall = (ImageView) convertView.findViewById(R.id.img_item_phone_call);
			holder.ivSMS = (ImageView) convertView.findViewById(R.id.img_item_phone_sms);
			
			convertView.setTag(holder);
		} else {
			holder = (Holder)convertView.getTag();
		}

		holder.tvName.setText(model.label + "");
		holder.tvNumber.setText(model.number);
		
		if (mPhoneType == PhoneType.Phone) {
			holder.ivCall.setVisibility(View.VISIBLE);
			holder.ivSMS.setVisibility(View.VISIBLE);
			holder.ivCall.setOnClickListener(this);
			holder.ivSMS.setOnClickListener(this);
		} else {
			holder.ivCall.setVisibility(View.GONE);
			holder.ivSMS.setVisibility(View.GONE);
		}
		
		
		return convertView;
	}	
	
	
	public void updateView(List<PhoneModel> list) {
		this.list = list;
		this.notifyDataSetChanged();
	}
	
	class Holder {
		public TextView tvName;
		public TextView tvNumber;
		public ImageView ivCall;
		public ImageView ivSMS;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_item_phone_call:
			
			break;
		case R.id.img_item_phone_sms:
			
			break;

		default:
			break;
		}
	}

}
